<?php
/**
 * Usage sampe:
<code>
//Creating and outputing image
$c = new DomCaptcha('MyAlias');
$c->width = 100;
$c->height = 40;
$c->characters = 5;
$c->show();

// validating
$c->validate( $_POST['code'], 'MyAlias' );
</code>
 * 
 * @package DomBase
 * @author Oliver Leisalu, Inditel Meedia OÜ
 */
class DomCaptcha {
	/**
	 * @var String
	 */
	private $font;
	/**
	 * @var Int
	 */
	private $width = 80;
	/**
	 * @var Int
	 */
	private $height = 40;
	/**
	 * @var Int
	 */
	private $characters = 4;
	/**
	 * @var String
	 */
	private $code;
	/**
	 * Alias is used if more than one captcha is used on webpage. 
	 * Set different aliases to validate different captchas that are shown at the same time.
	 * 
	 * @var String
	 */
	private $alias;
	
	const SESSION_VAR = 'DomCaptchaCode';
	
	const DEFAULT_ALIAS = 'default';
	/**
	 * @param String $alias
	 */
	public function __construct( $alias = self::DEFAULT_ALIAS ) {
		$this->font = dirname(__FILE__) . '/monofont.ttf';
		$this->alias = $alias;
	}
	/**
	 * @param String $var
	 * @param Mixed $value
	 */
	public function __set( $var, $value ) {
		$this->$var = $value;
	}
	/**
	 * @param Mixed $var
	 * @return String
	 */
	public function __get( $var ) {
		return $this->$var;
	}
	/**
	 * @param String $code
	 * @param String $alias
	 * @return Bool
	 */
	public static function validate( $code, $alias = self::DEFAULT_ALIAS ) {
		if( !$_SESSION[self::SESSION_VAR][$alias] ) {
			return false;
		}
		$result = $code == $_SESSION[self::SESSION_VAR][$alias];
		unset($_SESSION[self::SESSION_VAR][$alias]);
		return $result;
	}
	/**
	 * Generates code
	 */
	private function generateCode() {
		/* list all possible characters, similar looking characters and vowels have been removed */
		$possible = '23456789bcdfghjkmnpqrstvwxyz';
		$code = '';
		$i = 0;
		while ($i < $this->characters) { 
			$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
			$i++;
		}
		$this->code = $code;
		$_SESSION[self::SESSION_VAR][$this->alias] = $this->code;
	}
	/**
	 * Shows captcha image. Sets correct headers etc..
	 */
	public function showImage() {
		if( !$this->code )
			$this->generateCode();

		/* font size will be 75% of the image height */
		$font_size = $this->height * 0.75;
		$image = imagecreate($this->width, $this->height);
		/* set the colours */
		$background_color = imagecolorallocate($image, 255, 255, 255);
		$text_color = imagecolorallocate($image, 20, 40, 100);
		$noise_color = imagecolorallocate($image, 100, 120, 180);
		/* generate random dots in background */
		for( $i=0; $i<($this->width*$this->height)/3; $i++ ) {
		   imagefilledellipse($image, mt_rand(0,$this->width), mt_rand(0,$this->height), 1, 1, $noise_color);
		}
		/* generate random lines in background */
		for( $i=0; $i<($this->width*$this->height)/150; $i++ ) {
		   imageline($image, mt_rand(0,$this->width), mt_rand(0,$this->height), mt_rand(0,$this->width), mt_rand(0,$this->height), $noise_color);
		}
		/* create textbox and add text */
		$textbox = imagettfbbox($font_size, 0, $this->font, $this->code);
		$x = ($this->width - $textbox[4])/2;
		$y = ($this->height - $textbox[5])/2;
		imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $this->code);
		/* output captcha image to browser */
		header('Content-Type: image/jpeg');
		imagejpeg($image);
		imagedestroy($image);
   }
	
	
}
?>